const noon = "上午";
const afterNoon = "下午";
const ANBoundary = 12;
const ADay = 1000*60*60*24;

const jdbcOptions = {
	name: 'foot',
	path: '_doc/foot.db',
};

const TypeMap = new Map([
	["map",{apply:(param)=> new Map(param)}],
	[undefined,{apply:param=>param}],
]);

function dateSimpleOperation(paramDate = new Date() , paramAmount)
{
	paramDate = new Date(paramDate);
	if(!paramAmount)
	{
		console.log("量必须填");
		return;
	}
	paramDate.setTime(paramDate.getTime() + (ADay * paramAmount))
	return paramDate;
}

//日期属性转换
function dateToCastChineseTime(param = new Date())
{
	console.log("[utils.js]  dateToCastChineseTime()---- param:",param)
	let date = param;
	let hours = date.getHours();
	let minutes = date.getMinutes();
	let seconds = date.getSeconds();
	
	return (hours>=ANBoundary?afterNoon:noon) + hours +　":"　+ minutes + ":" + seconds;
}

//日期拼接 年月日星期
function dateAttributeMergeToString(paramDate = new Date(),paramPlaceHolder)
{
	console.log("[utils.js]  dateAttributeMergeToString()---- paramDate:",paramDate,"paramPlaceHolder:",paramPlaceHolder);
	const year = paramDate.getFullYear();
	const month = paramDate.getMonth();
	const date =  paramDate.getDate();
	const day = paramDate.getDay();
	let restString = "";
	if(paramPlaceHolder)
	{
		restString += year; restString +=paramPlaceHolder;
		restString += month; restString +=paramPlaceHolder;
		restString += date; restString +=paramPlaceHolder;
		restString += day; 
		return restString;
	}
	restString += year;
	restString += month;
	restString += date;
	restString += day;
	return restString;
	
}



// local storage upload
function uploadLocalStorage(paramKey,paramValue,paramType)
{
	console.log("[utils.js]  uploadLocalStorage()---- paramKey:",paramKey,"paramValue:",paramValue,"paramType:",paramType);
	if(!(paramKey && paramValue))
	{
		console.log("上传失败");
		return;
	}
	let k = paramKey;
	let v = paramValue;
	uni.setStorage({
	    key: k,
	    data: v,
	    success: function () {
	        console.log('uploadLocalStorage is success');
	    },
		fail(e) {
			console.log("uploadLocalStorage is fail");
		},
			
	});
	
}

// get local storage 
function getLocalStorage(paramKey,paramType)
{
	console.log("[utils.js]  getLocalStorage()---- paramKey:",paramKey,"paramType",paramType);
	if(!paramKey)
	{
		return null;
	}
	let k = paramKey;
	let restData;
	uni.getStorage({
	    key: k,
	    success: function (res) {
			console.log(res.data)
			restData = TypeMap.get(paramType).apply(res.data);
	    },
		fail(e) {
			console.log(e,"本地数据获取失败,数据可能不存在或被清空.");
		}
	});
	return restData;
}


//remove local storage
function removeLocalStorage(paramKey)
{
	console.log("[utils.js]  removeLocalStorage()---- paramKey:",paramKey);
	if(paramKey)
	{
		uni.removeStorage({
		    key: paramKey,
		    success: function (res) {
		        console.log('removeLocalStorage is success');
		    }
		});
		return;
	}
}

function sqlliteInitialize()
{
	
}

function sqlliteIsExist()
{
	let dbBoolean = plus.sqlite.isOpenDatabase(jdbcOptions);
	let data;
	if(!dbBoolean)
	{
		plus.sqlite.openDatabase({
				name: 'foot',
				path: '_doc/foot.db',
				success: function(e){
					console.log('openDatabase success!');
					dbBoolean = true;
				},
				fail: function(e){
					console.log('openDatabase failed: '+e);
					dbBoolean = false;
				}
		});
	}
	if(dbBoolean)
	{
		plus.sqlite.selectSql({
				name: 'foot',
				sql: 'select * from foot',
				success: function(data){
					console.log('selectSql success: ');
					for(var i in data){
						console.log(data[i]);
					}
				},
				fail: function(e){
					console.log('selectSql failed: '+JSON.stringify(e));
					if(e.message.indexOf("no such table") != -1)
					{
						//数据库不存在创建数据库
						plus.sqlite.executeSql({
								name: 'foot',
								sql: 'create table if not exists foot("id" INTEGER PRIMARY KEY AUTOINCREMENT,"name" VARCHAR(100),"week" INT(10),"type" VARCHAR(10))',
								success: function(e){
									console.log('executeSql success!');
								},
								fail: function(e){
									console.log('executeSql failed: '+JSON.stringify(e));
								}
							});
					}
				}
			});
	}
	else{
		console.log("sqllite 连接失败,请确认系统正常");
	}
	
}

function test()
{
	
}



module.exports = {
	dateToCastChineseTime: dateToCastChineseTime, 
	uploadLocalStorage: uploadLocalStorage,       
	getLocalStorage: getLocalStorage,
	dateAttributeMergeToString: dateAttributeMergeToString,
	removeLocalStorage: removeLocalStorage,
	sqlliteIsExist: sqlliteIsExist,
	test: test,
	dateSimpleOperation: dateSimpleOperation,
}